package com.chilly.db1.service;

import com.chilly.bean.Account;
import com.chilly.db1.dao.AccountDao;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrdersService {

    @Autowired
    private AccountDao accountDao;
//    public void setOrdersDao(AccountDao accountDao) {
//        this.accountDao = accountDao;
//    }

    @Transactional(propagation = Propagation.REQUIRED,
            isolation = Isolation.DEFAULT, readOnly = false, timeout = -1)
    public void accountMoney(String xiaohong, String xiaoming) {
        //小红账户多1000
        accountDao.addMoney(1000, xiaohong);
        //模拟突然出现的异常，比如银行中可能为突然停电等等
        //如果没有配置事务管理的话会造成，小红账户多了1000而小明账户没有少钱
//        int i = 10 / 0;
        //小王账户少1000
        accountDao.reduceMoney(1000, xiaoming);
    }

    @Transactional
    public void test(){

    }
    public void test3(){
        Object proxy = AopContext.currentProxy();

        test();
    }
}
